package com.sig.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

/**
 * 国际化工具
 *
 * @author sig
 * @since 2019/1/26
 */
public final class I18nUtil {
    private static ResourceBundle bundle;

    static {
//        bundle = ResourceBundle.getBundle("res", Locale.SIMPLIFIED_CHINESE);
        bundle = ResourceBundle.getBundle("res", Locale.US);
    }

    private I18nUtil() {
    }

    /**
     * @param key  the key for the desired string
     * @param args object(s) to format
     * @return
     */
    public static Object get(final String key, final Object... args) {
        if (bundle.containsKey(key))
            return MessageFormat.format(bundle.getString(key), args);
        return null;
    }

    public static void reload() {
        try {
//            bundle = new PropertyResourceBundle(new FileInputStream("bin/res_zh_CN.properties"));
            bundle = new PropertyResourceBundle(new FileInputStream("bin/res_en_US.properties"));
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}
